package com.monster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monster.dto.CheckGroupDTO;
import com.monster.entity.PageResult;
import com.monster.entity.QueryPageBean;
import com.monster.mapper.CheckGroupMapper;
import com.monster.pojo.CheckGroup;
import com.monster.pojo.CheckItem;
import com.monster.service.CheckGroupService;
import com.monster.service.CheckItemService;
import com.monster.utils.redis.RedisUtil;
import com.monster.utils.resources.RedisConstant;
import com.monster.vo.CheckGroupVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    CheckItemService checkItemService;


    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> page;
        QueryWrapper<CheckGroup> checkGroupQueryWrapper = new QueryWrapper<>();
        checkGroupQueryWrapper.eq("IS_DELETE", 0);
        //无条件查寻
        if (queryPageBean.getQueryString() == null) {
            page = this.page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), checkGroupQueryWrapper);
            return new PageResult(page.getTotal(), page.getRecords());
        }
        //有条件查询
        else {
            checkGroupQueryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            page = this.page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), checkGroupQueryWrapper);
            return new PageResult(page.getTotal(), page.getRecords());
        }
    }
    //
    @Override
    public Boolean addOrUpdataCheckGroupDTO(CheckGroupDTO checkGroupDTO) {
        //若是修改CheckGroupDTO会有id
        Integer groupDTOId = checkGroupDTO.getId();
        if(groupDTOId != null){
            //修改前先删除对应的表的值
            baseMapper.deleteCheckgroupCheckitem(groupDTOId);
        }
        //进行修改或者增加
        boolean save = this.saveOrUpdate(checkGroupDTO);
        Integer gid = checkGroupDTO.getId();
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        //循环添加
        if(checkitemIds != null && checkitemIds.length >0){
            for (Integer cid : checkitemIds) {
                baseMapper.addCheckgroupCheckitem(gid,cid);
            }
        }
        return save;
    }
    //数据回显
    @Override
    public CheckGroupVO showDataByGid(int id) {
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        List<CheckItem> checkItemList = checkItemService.findAll();
        checkGroupVO.setCheckItemList(checkItemList);
        List<Integer> findcheckitems = baseMapper.findcheckitems(id);
        checkGroupVO.setCheckItemIdList(findcheckitems);
        return checkGroupVO;
    }
    //数据删除
    @Override
    public Boolean deleteById(int id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        int type = baseMapper.updateById(checkGroup);
        return type != 0;
    }

}
